 

import java.io.File;
import javax.swing.ImageIcon;
import java.awt.Image;


import java.net.URL;

public class GraphicsHelper
{

    /**
     * Hilfsmethode zum Umwandeln eines Dateinamens in die entsprechende URL.
     * Statische Variante.
     */
    public static URL getURL(String filename) {
        //return GraphicsHelper.class.getResource(filename);
        //return GraphicsHelper.class.getClassLoader().getResource(filename);
        Class currentClass = new Object() { }.getClass().getEnclosingClass();
        return currentClass.getResource(filename);
    }    

    /**
     * Hilfsmethode zum sicheren Erzeugen eines Image aus einem Dateinamen.
     */
    public static Image createImage(String filename) {
        URL url = getURL(filename);
        java.awt.image.BufferedImage image = null;
        try {
            image = javax.imageio.ImageIO.read(url);
        }
        catch (Exception e) {
            System.out.println("Error in GraphicsHelper: "+e);
        }
        return image;
    }

    /**
     * Hilfsmethode zum sicheren Erzeugen eines ImageIcon aus einem Dateinamen.
     */
    public static ImageIcon createImageIcon(String filename) { 
        URL url = getURL(filename);
        return new ImageIcon(url);
    }

    static Image resizeImage(Image image, int x, int y) {    
        Image newimg = image.getScaledInstance(x, y, java.awt.Image.SCALE_SMOOTH); // alternativ: SCALE_FAST 
        return newimg;
    }

    static ImageIcon resizeImageIcon(ImageIcon icon, int x, int y) {    
        Image image = icon.getImage();
        image = resizeImage(image, x, y);
        return new ImageIcon(image);
    }


    static Image getImageFromSpritesheet(String filename, int x, int y, int width, int height) {
        java.awt.image.BufferedImage image = null;
        try {
            image = javax.imageio.ImageIO.read(GraphicsHelper.getURL(filename));
        }
        catch (Exception e) {
            System.out.println(e);
        }
        return image.getSubimage(x,y,width,height);
    }

    
    //Noch zu testen:


    /**
    //If you just want to draw the sprites, Java's Graphics class has a drawImage method 
    //that will pull a specific area of the image out for you. You just have to specify the source image, 
    //where you want to draw the sprite on your Graphics object (x, y, width, height), and in what frame 
    //of the image the sprite is located (x, y, width, height).

    //http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/Graphics.html#drawImage%28java.awt.Image,%20int,%20int,%20int,%20int,%20int,%20int,%20int,%20int,%20java.awt.image.ImageObserver%29    **/
    // void drawSpriteFrame(Image source, Graphics2D g2d, int x, int y,
    //                      int columns, int frame, int width, int height)
    // {
    //     int frameX = (frame % columns) * width;
    //     int frameY = (frame / columns) * height;
    //     g2d.drawImage(source, x, y, x+width, y+height,
    //                   frameX, frameY, frameX+width, frameY+height, this);
    // }    

    static void drawScaledSpriteFromFrame(
    Image spritesheet, java.awt.Graphics2D g2d, 
    int x1goal, int y1goal,
    int x2goal, int y2goal,
    int x1source, int y1source, 
    int x2source, int y2source
    ) {
        g2d.drawImage(spritesheet, x1goal, y1goal, x2goal, y2goal, x1source, y1source, x2source, y2source, null);                
    }

}
